#include "AABB.h"

/// <Summary> The collision type of this object </Summary>
const HashString AABB::COLLISIONTYPE("COLLISION_AABB");

////////////////////////////////////////////////////////////////////////////////////////////////////
/// <Summary>	Builds the bounding volume from a set of points </Summary>
///
/// <Param name="verts">   	The verts </Param>
/// <Param name="numVerts">	Number of verts </Param>
////////////////////////////////////////////////////////////////////////////////////////////////////
void AABB::Build(const Vector3* verts, unsigned int numVerts)
{
	minPoint.x = minPoint.y = minPoint.z = FLT_MAX;
	maxPoint.x = maxPoint.y = maxPoint.z = -FLT_MAX;

	for(unsigned int i = 0; i < numVerts; ++i)
	{
		for(unsigned int j = 0; j < 3; ++j)
		{
			minPoint.arr[j] = min( minPoint.arr[j], verts[i].arr[j] );
			maxPoint.arr[j] = max( maxPoint.arr[j], verts[i].arr[j] );
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// <Summary>	
/// 	Builds the bound volume from two other bounding volumes of the same type 
/// </Summary>
///
/// <Param name="bounds1">	The first bounds </Param>
/// <Param name="bounds2">	The second bounds </Param>
////////////////////////////////////////////////////////////////////////////////////////////////////
void AABB::Build(const IBoundingCollideAble* bounds1, const IBoundingCollideAble* bounds2)
{
	if( bounds1->GetCollisionType() != AABB::COLLISIONTYPE || bounds2->GetCollisionType() != AABB::COLLISIONTYPE )
		return;

	AABB* box1 = (AABB*)bounds1;
	AABB* box2 = (AABB*)bounds2;

	for(unsigned int i = 0; i < 3; ++i)
	{
		minPoint.arr[i] = min( box1->minPoint.arr[i], box2->minPoint.arr[i] );
		maxPoint.arr[i] = max( box1->maxPoint.arr[i], box2->maxPoint.arr[i] );
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// <Summary>	Prints this object to the console </Summary>
///
/// <Param name="out">	The console window </Param>
///
/// <Return>	The console window </Return>
////////////////////////////////////////////////////////////////////////////////////////////////////
ConsoleWindow& AABB::Print(ConsoleWindow& out) const
{
	return out << "\nAABB:\n" << "MinPoint:  " << minPoint << "\nMaxPoint:  " << maxPoint << "\n";
}